<template>
  <div class="app-container">
    <doc-alert title="系统日志" url="https://doc.iocoder.cn/system-log/" />
    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="108px">
      <el-form-item label="用户姓名" prop="nickname">
        <el-input v-model="queryParams.nickname" placeholder="请输入用户姓名" clearable style="width: 240px;"
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="用户编码" prop="username">
        <el-input v-model="queryParams.username" placeholder="请输入用户编码" clearable style="width: 240px;"
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="所属组织" prop="deptName">
        <el-input v-model="queryParams.deptName" placeholder="请输入所属组织" clearable style="width: 240px;"
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="登录/登出平台" prop="openPlatform">
        <el-select v-model="queryParams.openPlatform" placeholder="请选择登录/登出平台" clearable style="width: 240px">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.OPERATIONLOGIN_TYPE)" :key="dict.label"
                     :label="dict.label" :value="dict.label"/>
        </el-select>
      </el-form-item>
      <el-form-item label="登录/登出状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择登录/登出状态" clearable style="width: 240px">
          <el-option :key="1" label="成功" :value="1"/>
          <el-option :key="0" label="失败" :value="0"/>
        </el-select>
      </el-form-item>
      <el-form-item> 
        <el-button v-preventReClick type="primary" icon="el-icon-search" @click="handleQuery">查询</el-button>
        <el-button v-preventReClick icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5" style="margin-left:40px ;">
        <el-button v-preventReClick type="primary"  size="mini" @click="handleExport" :loading="exportLoading"
                   v-hasPermi="['system:login-log:export']">导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-preventReClick type="primary"  size="mini" @click="handleExportHistory" :loading="exportLoading"
                   v-hasPermi="['system:login-log:export']">导出数据</el-button>
      </el-col>
      <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
    </el-row>

    <el-table v-loading="loading" :data="list" style="margin-top:8px" >
      <el-table-column label="用户姓名" align="center" prop="nickname" />
      <el-table-column label="用户编码" align="center" prop="username" />
      <el-table-column label="所属组织" align="center" prop="deptName" />
      <el-table-column label="登录IP" align="center" prop="userIp" />
      <el-table-column label="浏览器" align="center" prop="userAgent" />
      <el-table-column label="操作系统" align="center" prop="os" />    
      <el-table-column label="登录/登出" align="center" prop="logType" />     
      <el-table-column label="所属平台" align="center" prop="openPlatform" width="150" :show-overflow-tooltip="true" />
      <el-table-column label="登录/登出状态" align="center" prop="result" width="180" :show-overflow-tooltip="true" >
        <template v-slot="scope">
          <span v-if="scope.row.result == 0">成功</span>
          <span v-else>失败</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="登录/登出时间" align="center" prop="createTime" /> -->
      <el-table-column label="登录/登出时间" align="center" prop="createTime" >
          <template slot-scope="scope">
              <span >{{ parseTime(scope.row.createTime) }}</span>
            </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>
    <!-- 数据导出详细 -->
    <div v-if="showType==1">
    <el-dialog  title="数据导出" :visible.sync="open" width="700px" append-to-body :before-close="close">
      <el-row class="margin-top-sm padding-left padding-right height500">
            <el-col :span="24">
                <el-steps :active="active">
                <el-step title="数据导出"></el-step>
                <el-step title="导出完成"></el-step>
                </el-steps>
            </el-col>
      </el-row>
      <el-row class="margin-top-sm padding-left padding-right height500">
            <el-col :span="24">
              <div v-if="active == 0">
                <div class="text-center">
                    <div><i style="font-size:50px" class="el-icon-loading"></i></div>
                    <div class="margin-top-xs">数据导出中</div>
                    <div style="margin-top:70px;">当前数据正在导出，您可以点击[最小化]隐藏该页面，数据导出不受影响</div>
                </div>
              </div>
              <div v-if="active == 1">
                <div class="text-center">
                  <div v-if="errorNum!=0"><i style="font-size:50px;color:#FFBE00 ;"  class="el-icon-warning"></i></div>
                    <div v-if="errorNum==0"><i style="font-size:50px;color:#1890FF;"  class="el-icon-success"></i></div>
                    <!-- <div class="margin-top-xs">数据导出成功</div> -->
                    <div style="margin-top:70px;">导出总条数{{ totalNum }}条，导出成功<span style="color:#1890FF ;">{{ successNum }}</span>条，导出失败<span style="color:red ;">{{ errorNum }}</span>条</div>
                </div>
              </div>
            </el-col>
      </el-row>
      <div slot="footer" class="border-top padding-top">
        <el-button v-preventReClick v-if="active == 0" type="primary"  @click="changeSize('min')">最小化</el-button>
        <el-button v-preventReClick v-if="active == 1"   @click="close">取消</el-button>
        <el-button v-preventReClick v-if="active == 1" type="primary"  @click="close">确认</el-button>
      </div>
    </el-dialog>
  </div>
    <el-drawer
          title="导出数据列表"  append-to-body    :visible.sync="drawer" size="70%"
          :with-header="false" direction="rtl" custom-class="my-drawer" >
          <img style="width: 34px;margin-left: -35px;  margin-top: 2px;" src="../../assets/images/close.png" alt @click="drawer=false" >
            <p style="display: inline; color:black;font-size: 16px; margin-left: 15px">导出数据列表</p>
            <el-tooltip class="item" effect="dark" content="仅展示近七天的导出数据" placement="bottom"> 
              <div class="icon"></div>
            </el-tooltip>
          <div>
              <el-table ref="table" height="650" :data="datelist" show-overflow-tooltip class="margin-top" >
                <el-table-column label="序号" align="center" prop="id" />
                <el-table-column label="文件名称" align="center" prop="fileName" />
                <el-table-column label="文件状态" align="center" prop="fileStatus" />
                <el-table-column label="操作人" align="center" prop="creatorNickname" />
                <el-table-column label="操作时间" align="center" prop="createTime" >
                  <template slot-scope="scope">
                      <span >{{ parseTime(scope.row.createTime) }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="处理完成时间" align="center" prop="finishTime" >
                  <template slot-scope="scope">
                      <span >{{ parseTime(scope.row.finishTime) }}</span>
                    </template>   
                </el-table-column> 
                <el-table-column label="文件大小" align="center" prop="fileSize" >
                   <template slot-scope="scope">
                      <span >{{ scope.row.fileSize }}kb</span>
                    </template>
                </el-table-column>
                <el-table-column label="总条数" align="center" prop="fileTotalCount" />
                <el-table-column label="导出失败条数" align="center" prop="fileFailCount" />
                <el-table-column label="操作" align="center" prop="operation" >
                  <template v-slot="scope">
                    <i style="color:#1890FF" class="el-icon-download" @click="downloadFile(scope.row)" ></i>
                  </template>
                </el-table-column>
              </el-table>
                <pagination v-show="drawerTotal>0" :total="drawerTotal" :page.sync="queryHistoryParams.pageNo" :limit.sync="queryHistoryParams.pageSize"
                 @pagination="getHistoryList" />       
          </div>                
    </el-drawer>
    <div v-if="showType==2" class="types" @click="changeSize('max')">
        <div class="postion"  v-if="minloading">
            <i class="el-icon-loading"></i>
            导出中
        </div>

        <div class="postion colors" v-if="!minloading">
            <i class="el-icon-check"></i>
            导出完成
        </div>
    </div>
  </div>
</template>

<script>
import axios from 'axios'
import { getLoginLogPage,exportLoginLog,getLoginLogHistoryPage,downloadExportLoginLogHistory,getExportResult_1} from "@/api/infra/manLoginLog"
import { getDictDatas, DICT_TYPE } from '@/utils/dict'


export default {
  name: "SystemLoginLog",
  data() {
    return {
      totalNum:0,
      successNum:0,
      errorNum:0,
      minloading:true,
      //导出数据弹窗状态
      showType:1,
      //导出数据弹窗
      open:false,
      //导出数据列表
      drawer:false,
      //导出数据列表状态
      active:0,
      // 导出遮罩层
      exportLoading: false,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //drawer总条数
      drawerTotal:0,
      // 表格数据
      list: [],
      //导出数据列表数据
      datelist:[],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        username: '',
        nickname: '',
        status: undefined,
       // createTime: [],
        deptName:'',
        openPlatform: undefined,
      },
      queryHistoryParams :{
        pageNo: 1,
        pageSize: 10,
      },
      axiosCancleToken: '',
      // 数据字典
      statusDictDatas: getDictDatas(DICT_TYPE.OPERATIONLOGIN_TYPE)
    };
  },
  created() {
    this.getList();
    this.getHistoryList()
  },
  methods: {
    close(){
      this.open = false 
      this.active = 0 
      this.showType = 1
      if (this.axiosCancleToken) {
        this.axiosCancleToken('home page get list interface repeat')
      }
      this.exportLoading = false;
      this.axiosCancleToken = axios.CancelToken.source()
    },
    /** 查询登录日志列表 */
    getList() {
      this.loading = true;
      console.log(this.queryParams,'queryParams---')
      getLoginLogPage(this.queryParams).then(response => {
          
          this.list = response.data.list;
          this.list.forEach(item => {
            if(item.logType==200||item.logType==202){
              item.logType='登出'
            }else{
              item.logType='登录'
            }
          }); 
          this.total = response.data.total;
          this.loading = false;
        }
      );
    },
    /** 查询导出登录日志历史列表 */
    getHistoryList() {
     //this.loading = true;
      getLoginLogHistoryPage(this.queryHistoryParams).then(response => {        
          this.datelist = response.data.list;
          this.datelist.forEach(item => {
            if(item.fileStatus==true){
              item.fileStatus='导出成功'
            }else{
              item.fileStatus='导出失败'
            }
          }); 
          this.drawerTotal = response.data.total;
        }        
      );
    },
    /*下载当前行的文件*/
    downloadFile(row){
      console.log(row,'row8888---')
      downloadExportLoginLogHistory(row.id).then(response => {
        this.$download.excel(response, '导出过的登录日志.xls');  
      })     
    },
    /** 最小化或最大化文件导出页面弹窗 */
    changeSize(type){
      if(type=='min'){
        this.open = false
        this.showType=2
      }else{
        this.open = true
        this.showType=1
      }   
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      this.$modal.confirm('是否确认导出所有登录登出日志数据项?').then(() => {
          // 处理查询参数
          let params = {...this.queryParams};
          params.pageNo = params.pageNo?params.pageNo:undefined;
          params.pageSize = params.pageSize?params.pageSize:undefined;
          params.nickname = params.nickname?params.nickname:undefined;
          params.username = params.username?params.username:undefined;
          params.deptName = params.deptName?params.deptName:undefined;
          params.openPlatform = params.openPlatform?params.openPlatform:undefined;
          params.status = params.status?params.status:undefined;
          params.traceId = params.traceId?params.traceId:undefined;
          params.Authorization = params.Authorization?params.Authorization:undefined;
          this.exportLoading = true;
          this.open = true;
          exportLoginLog(params,
           new axios.CancelToken(c => {
          this.axiosCancleToken = c
        })
          ).then(response=>{
            let failTag=decodeURI(response.headers['file-tag'])
            console.log(failTag,'failTag--1111111111-')
            getExportResult_1(failTag).then(response => {  
              this.totalNum=response.data.totalCount
              this.successNum=response.data.successCount
              this.errorNum=response.data.failCount
            });  
            this.exportLoading = false;
            this.active = 1 
          })
        }).then(response => {
      }).catch(() => {});
      //this.getHistoryList()
    },
    handleExportHistory(){
      this.drawer=true
      this.getHistoryList()
    }
  }
};
</script>
<style scoped lang="scss">
.postion{
    position: fixed;
    right: 0;
    bottom: 20%;
    width: 100px;
    border: 1px solid #ddd;
    line-height: 35px;
    height: 35px;
    color: #ddd;
    z-index: 999999999;
    background: #ffffff;
    padding-left: 10px;
    border-radius: 20px;
    font-size: 14px;
    i{
        margin-right: 3px;
    }
}
.colors{
    color: #1890ff;
}
.icon {
  margin-left: 5px;
  position: relative;
  display: inline-block;
  width: 18px;
  height: 18px;
  border: 2px solid #959595;
  border-radius: 50%;
  margin-bottom: -3px;
}

.icon::before {
  content: '!';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-family: Arial, sans-serif;
  font-weight: bold;
  font-size: 16px;
  color: #959595;
}
.border-top {
  border-top: 1px solid #ddd;
}
::v-deep .el-step__title {
  font-size: 14px;
}
::v-deep .el-step__title.is-process {
  color: #1257ff;
}
::v-deep .is-process {
  .el-step__icon {
    color: #ffffff;
    background: #1257ff;
    border-color: #ffffff;
  }
}
::v-deep .my-drawer {
  overflow: visible !important;
}

::v-deep .my-drawer section {
  overflow: visible !important;
}
</style>

